MOOC Utils

Utils like auto assignment solver extension and course notice announcment notification for MOOC courses

Services layer

Introduction#

Service layer behind the handlers:

  • Gemini integration: API key, model choice, request/response, answer parsing
  • Local storage for preferences and cache
  • Factory and dependency-injection wiring, with explicit errors
  • Config, rate limits, and how core components call in

Project structure#

The services layer is organized around two primary service factories:

  • Gemini service: orchestrates extraction and solving workflows with Google Generative Language API
  • Storage service: manages API keys, cached extractions, user answers, and model preferences

These services integrate with platform adapters (browser APIs), background workers, content scripts, and UI controllers.

Core components#

  • Gemini Service Factory
  • Builds content with HTML, images, and screenshots
  • Configures reasoning budgets per model family
  • Sends requests via background worker or direct API
  • Parses structured JSON responses with multiple fallback strategies
  • Storage Service Factory
  • Persists API key, cached extractions, user answers, and model preferences
  • Exports formatted answer sets for submission
  • Platform Adapters
  • Runtime adapter for cross-browser messaging
  • Tabs adapter for tab queries and content messaging
  • Storage adapter for browser local storage
  • Background Worker and Handlers
  • Initializes services and registers message handlers
  • Routes GEMINI_REQUEST to Gemini service
  • UI Settings Controller
  • Loads and saves API key and model preferences
  • Updates UI to reflect reasoning budget mapping

Architecture overview#

The services layer follows a factory pattern with explicit dependency injection. Background worker initializes platform adapters and services, then registers message handlers. Content scripts communicate via the runtime adapter. The Gemini service encapsulates API concerns and response parsing, while the storage service centralizes persistence.

Detailed component analysis#

Gemini service factory#

Responsibilities:

  • Build content parts from HTML, images, and screenshots
  • Configure thinking/budget reasoning per model family
  • Generate structured payloads with system instructions and response schemas
  • Send requests via background worker with retry logic or directly for background-only calls
  • Parse responses with multiple strategies and error reporting

Key behaviors:

  • Reasoning budget mapping and model filtering for thinking support
  • Payload construction with system instructions and response schemas
  • Two transport modes:
  • callAPI: routed through background worker for UI-initiated requests
  • directAPICall: used by background worker to avoid message channel timeouts
  • Reliable parsing with multiple fallbacks and truncation repair

Storage service factory#

Responsibilities:

  • Persist and retrieve API key
  • Manage extraction cache with URL and timestamp
  • Store and load user answers
  • Save and load model preferences with defaults
  • Export formatted answer sets for submission

Design highlights:

  • Centralized persistence via storage adapter
  • Defaults for model preferences if missing
  • Export helpers for answer-only datasets

Background worker and handlers#

Responsibilities:

  • Initialize logger and platform adapters
  • Create and wire services (Gemini, Screenshot)
  • Register message router and handlers
  • Route GEMINI_REQUEST to Gemini service for direct API calls

Integration points:

  • Runtime adapter for cross-browser messaging
  • Tabs adapter for tab queries and content messaging
  • Gemini handler executes direct API calls from background context

UI settings controller#

Responsibilities:

  • Load stored API key and model preferences into form controls
  • Save API key and model preferences to storage
  • Update UI labels reflecting reasoning budget mapping

Integration:

  • Depends on storage service for persistence
  • Drives model preference updates used by Gemini service

Dependency analysis#

The services layer uses a clean dependency injection pattern:

  • Background worker composes services with platform adapters
  • Gemini service depends on runtime adapter for messaging and on parser/schema for response handling
  • Storage service depends on storage adapter for persistence
  • UI controller depends on storage service for settings

Performance considerations#

  • Thinking budget and reasoning levels
  • Gemini supports reasoning budgets for supported models; unsupported models skip thinking configuration
  • Budget mapping caps maximum thinking budget per reasoning level
  • Image size handling
  • Large images are skipped to prevent exceeding API constraints
  • Retry and connection resilience
  • Message sending includes exponential backoff for transient connection errors
  • Direct API calls from background
  • Background worker uses direct fetch to bypass message channel timeouts

Recommendations:

  • Prefer background-only direct calls for heavy payloads
  • Monitor finish reasons and adjust reasoning levels to balance quality and cost
  • Cache extractions to reduce redundant API calls

Troubleshooting guide#

Common issues and resolutions:

  • API key errors
  • Ensure API key is saved via settings and retrieved by storage service
  • Verify model preferences are set appropriately
  • Parsing failures
  • Parser attempts multiple strategies; check logs for trimmed content and finish reasons
  • For MAX_TOKENS, truncated JSON repair is attempted
  • Connection errors
  • sendMessageWithRetry handles transient connection failures; inspect logs for repeated errors
  • Blocked or empty responses
  • Blocked prompts surface block reasons; empty candidates trigger errors

Operational tips:

  • Use GEMINI_DEBUG messages to inspect payloads in content scripts
  • Confirm background worker initialization and handler registration
  • Validate browser compatibility via platform adapters

Conclusion#

Services hide Gemini and storage details from handlers. Keep HTTP and prompt logic here so handlers stay about messaging.

Appendices#

Configuration options#

  • Model selection
  • Extraction model and solving model IDs
  • Reasoning levels: none, low, medium, high
  • Defaults
  • Extraction model defaults to a supported 2.5-family model
  • Solving model defaults to a supported 3.0-family model
  • Reasoning levels default to high for both tasks

Rate limiting implementation#

  • Client-side retry with backoff for transient connection errors
  • No explicit external API rate limiter is implemented in the services layer
  • Consider server-side quotas and adjust request frequency accordingly

Integration examples with core extension components#

  • Settings controller
  • Saves API key and model preferences to storage service
  • Loads stored values into UI controls
  • Background worker
  • Creates Gemini service with runtime adapter and logger
  • Registers GEMINI_REQUEST handler for direct API calls
  • Content script
  • Receives UI commands and interacts with page DOM
  • Supports debug relaying for Gemini payloads